package com.fernfx.xingtan.main.ui;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.fernfx.xingtan.R;
import com.fernfx.xingtan.common.base.BaseActivity;
import com.fernfx.xingtan.common.base.BaseFragment;
import com.fernfx.xingtan.common.entity.user.MemberEntity;
import com.fernfx.xingtan.main.contract.MyContract;
import com.fernfx.xingtan.main.entity.MyUserInfoEntity;
import com.fernfx.xingtan.main.presenter.MyPresenter;
import com.fernfx.xingtan.my.ui.MyPrincipalActivity;
import com.fernfx.xingtan.my.ui.MyPurseActivity;
import com.fernfx.xingtan.my.ui.MySettingActivity;
import com.fernfx.xingtan.my.ui.SettingApplicationActivity;
import com.fernfx.xingtan.user.AccountManager;
import com.fernfx.xingtan.utils.GlideUtil;
import com.fernfx.xingtan.utils.OtherUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by admin on 2018/8/13.
 * 我的
 */
public class MyFragment extends BaseFragment implements View.OnClickListener,MyContract.View{
    @BindView(R.id.tv_common_title)
    TextView titleTv;
    @BindView(R.id.user_logo_iv)
    ImageView userLogoIv;
    @BindView(R.id.user_nickname_tv)
    TextView nickNameTv;
    @BindView(R.id.user_account_tv)
    TextView userAccountTv;

    //presenter
    private MyContract.Presenter presenter;

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_my;
    }

    @Override
    protected void initView() {
        presenter = new MyPresenter();
        presenter.init(this);
        //register eventbus
        EventBus.getDefault().register(this);
    }

    @Override
    protected void initData() {
        requestArgsMap.clear();
        presenter.getUserInfo(requestArgsMap);
    }

    @OnClick({R.id.setting_tv,R.id.my_setting_rlt,R.id.my_purse_rlt,R.id.my_customer_rlt})
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.setting_tv:
                MySettingActivity.start(getActivity());
                break;
            case R.id.my_setting_rlt:
                SettingApplicationActivity.start(getActivity());
                break;
            case R.id.my_purse_rlt:
                MyPurseActivity.start(getActivity());
                break;
            case R.id.my_customer_rlt:
                MyPrincipalActivity.start(getActivity());
                break;
        }
     }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    @Override
    public void showUserInfo(MyUserInfoEntity userInfoEntity) {
        MyUserInfoEntity.ObjBean objBean = userInfoEntity.getObj();
        if (null == objBean) {
            return;
        }
        //显示头像
        String avatarImg = objBean.getAvatar();
        if (!TextUtils.isEmpty(avatarImg)) {
            GlideUtil.loadCircleImage(getActivity(), avatarImg, userLogoIv);
        }
        if (!TextUtils.isEmpty(objBean.getNickName())) {
            //昵称
            nickNameTv.setText(OtherUtil.checkEmptyDefault(objBean.getNickName()));
        }
        if (objBean.getUserId() != 0) {
            //账号
            String accountContent = userAccountTv.getText().toString();
            String accountText = String.format(accountContent, OtherUtil.checkEmptyDefault("" + objBean.getUserId()));
            userAccountTv.setText(accountText);
        }
    }
}
